package com.example.superyi.mainBar.fregments;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.superyi.mainBar.Adapter.HomePageAdapter;
import com.example.superyi.myteam.R;

public class MainFragment_HomePage extends Fragment {
    private SwipeRefreshLayout home_Refresh;
    private RecyclerView RC_Home1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_fg_homepage,container,false);
        Log.v("test","----------------运行到此处fragmentA_homepage");

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.v("test","----------------运行到此处fragment的onViewcreated");


    }
    //获取布局的小方法
    private void initView(){
        this.home_Refresh = getActivity().findViewById(R.id.home_Refresh);
        this.RC_Home1 = getActivity().findViewById(R.id.homepage_items);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        //模拟刷新过程
        home_Refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //模拟网络请求需要1500毫秒，请求完成，设置setRefreshing 为false
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        home_Refresh.setRefreshing(false);
                    }
                }, 1500);

            }
        });

        //items放置
        RC_Home1.setLayoutManager(new LinearLayoutManager(getContext()));
        RC_Home1.setAdapter(new HomePageAdapter(getContext(),new HomePageAdapter.OnItemClickListener(){
            @Override
            public void onClick(int pos) {
//用adpter中自己写的接口OnItemClickListener(),重写click方法
                Log.v("test","----------------点击到此处的view的position"+pos);
                Toast.makeText(getContext(),"----------------点击到此处的view的position"+pos,Toast.LENGTH_SHORT).show();
            }
        }));
    }
}

